package cycling.taxi.com.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import cycling.taxi.com.PopWindow.DetailsPopWindow;
import cycling.taxi.com.R;
import cycling.taxi.com.base.BaseFragment;
import cycling.taxi.com.base.LoadDataUi;
import cycling.taxi.com.entity.CommonQuestion;
import cycling.taxi.com.entity.Tips;
import cycling.taxi.com.ui.activity.AutofixActivity;
import cycling.taxi.com.ui.activity.AutofixItem;
import cycling.taxi.com.ui.activity.TipsItem;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;


public class AutofixFrgt extends BaseFragment implements AdapterView.OnItemClickListener{
    private ListView lv_qustion;
    private List<CommonQuestion> data;
    private RequestQueue queue;
    @Override
    protected View OnInitSuccessView() {
        View view = LayoutInflater.from(this.getContext()).inflate(R.layout.fragment_autofix_frgt, null);
        initUI(view);
        initClick();
        lv_qustion.setOnItemClickListener(this);
        return view;
    }
    private void initUI(View view) {
        lv_qustion=(ListView) view.findViewById(R.id.lv_qustion);
    }
    private void initClick(){
        queue= Volley.newRequestQueue(this.getContext());
        volleyGet();
    }
    public void volleyGet(){
        // String path="http://192.168.1.100:8080/CyclingBgTV1/appRepairByself/repairByself.dop";
        String path = Urls.getReparByself()+ "?beanJson="+SPUtil.getInstance().getData("beanJson").replace(" ","%20");
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                data = new Gson().fromJson(s,new TypeToken<List<CommonQuestion>>(){}.getType());
                lv_qustion.setAdapter(new MyQustionAdapter());
            }
        },null);
        queue.add(request);
    }
    @Override
    protected LoadDataUi.Result doInbackground() {
        return LoadDataUi.Result.SUCCESS;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent comItem= new Intent(this.getActivity(), AutofixItem.class);
        Adapter adpter=parent.getAdapter();
        CommonQuestion item=(CommonQuestion) adpter.getItem(position);//拿到当前数据值并强转   adpter.getItem(i)即为当前数据对象
        comItem.putExtra("CommonQuestion", item);

        comItem.setClass(this.getActivity(),AutofixItem.class);
        startActivity(comItem);

    }

    public class MyQustionAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.qustion_item, null);
            }
            CommonQuestion cq = data.get(position);
            TextView tv_qustion = (TextView) convertView.findViewById(R.id.tv_qustion);
            tv_qustion.setText(cq.getQuestionDescription());


            return convertView;
        }
    }
}
